package com.rts.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Random;

/**
 * 测试
 */
@Controller
@Slf4j
@RequestMapping(value = "/test")
public class TestController {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取随机数值-get
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/getRandomData")
    public Integer getRandomData(){

        Random random = new Random();
        // 生成100以内的随机数返回
        Integer randomInt = random.nextInt(100);
        log.info("[getRandomData] 获取的随机数 randomInt={}",randomInt);
        return randomInt;

    }

    /**
     * 获取随机数值-post
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/getPostRandomData")
    public Integer getPostRandomData(){

        Random random = new Random();
        // 生成100以内的随机数返回
        Integer randomInt = random.nextInt(100);
        log.info("[getPostRandomData] 获取的随机数 randomInt={}",randomInt);
        return randomInt;

    }

    /**
     * 模拟接收的数据-get
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/revGetData")
    public String revGetData(){

        log.info("[revGetData] 接收到get请求");
        return "云端接收到get请求，返回";

    }

    /**
     * 模拟接收的数据-post
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/revPostData")
    public String revPostData(){

        log.info("[revPostData] 接收到post请求");
        return "云端接收到post请求，返回";

    }

    /**
     * 测试获取redis数据
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/getRedisData")
    public String getRedisData(){

        log.info("[getRedisData] 测试获取redis数据");
        redisTemplate.opsForValue().set("test1","test2");
        String redisValue = (String) redisTemplate.opsForValue().get("test1");
        log.info("[getRedisData] redisValue={}",redisValue);
        return "云端getRedisData";

    }

}
